package com.project.erp.action.fobcost;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.project.erp.action.BaseAction;
import com.project.erp.common.utils.Page;
import com.project.erp.model.fobcost.FobCost;
import com.project.erp.model.fobcost.FobCostDetail;
import com.project.erp.service.fobcost.FobCostDetailService;
import com.project.erp.service.fobcost.FobCostService;

/**
 * 
 * COPYRIGHT (C) 2013 BY XXX公司

 *  功能模块名：FOB成本模板 action类

 *  功能描述：分发处理FOB成本模板操作请求

 * @author LiJiaGuo    

 * @version 1.0  

 * @created 2013-11-17 下午3:17:08
 */
@Controller
@Scope("prototype")
public class FobCostTpAction extends BaseAction implements ModelDriven<FobCost> {

	private static final long serialVersionUID = -8196013221188248002L;
	private FobCost fobCost;
	private String fobcostIds;
	private FobCostDetail fobCostDetail;
	private List<FobCostDetail> fobCostDetailList;
	private List<FobCost> fobCostList;//用于选择fob模板
	
	//细表字段
	private Long[] fobcostValueIds;
	private String[] itemEn;
	private String[] itemZn;
	private Boolean[] isBaseonInvo;
	private Double[] amount;
	
	@Autowired
	private FobCostService fobCostService;
	@Autowired
	private FobCostDetailService fobCostDetailService;
	
	public String toListFobCostTp() throws Exception{
		return SUCCESS;
	}
	
	public String getFobCostTpListData() throws Exception{
		logger.debug("entry getFobCostListData method ...");
		try{
			Page<FobCost> page = new Page<FobCost>();
			page.setPageNo(getPage());
			page.setPageSize(getRows());
			page.setOrderBy("createTime");
			page.setOrder(Page.DESC);
			fobCost.setIsTemplate(true);//增加模板标志，筛选模板记录
			page = fobCostService.getFobCostPageBySearch(page, fobCost);

			//设置表格返回数据
			this.getResultMap().put("total", page.getTotalCount());
			this.getResultMap().put("rows", page.getResult());
		}catch(Exception e){
			logger.error("execute getFobCostListData error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 选择FOB模板时，模板查询列表
	 * @return
	 * @throws Exception
	 */
	public String chooseListFobCostTp() throws Exception{
		logger.debug("entry chooseListFobCostTp method ...");
		
		return SUCCESS;
	}
	
	/**
	 * 添加FOB成本记录前,先选择FOB成本模板
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String toChooseFobCostTp() throws Exception{
		logger.debug("entry toChooseFobCostTp method!...");
		try{
			Page<FobCost> page = new Page<FobCost>();
			page.setPageNo(0);
			page.setPageSize(20);
			page.setOrderBy("createTime");
			page.setOrder(Page.DESC);
			fobCost.setIsTemplate(true);//增加模板标志，筛选模板记录
			page = fobCostService.getFobCostPageBySearch(page, fobCost);

			fobCostList = page.getResult();
		}catch(Exception e){
			logger.error("execute toChooseFobCostTp error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 选择FOB成本模板后，根据FOB的id取得对应FOB记录
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String chooseFobCostTp() throws Exception{
		logger.debug("entry chooseFobCostTp method!...");
		try{
			fobCost = fobCostService.getFobCostById(fobCost.getId());
		}catch(Exception e){
			logger.error("execute chooseFobCostTp error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 到添加FOB成本模板信息
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String toAddFobCostTp() throws Exception{
		logger.debug("entry toAddFobCost method!...");
		return SUCCESS;
	}
	
	/**
	 * 保存FOB成本模板信息
	 * @return SUCCESS
	 * @throws Exception 异常信息
	 */
	public String addFobCostTp() throws Exception{
		logger.debug("entry addFobCost method!...");
		try{
			
			fobCostDetailList = new ArrayList<FobCostDetail>();
			if(itemEn != null){
				//设置细表信息
				for(int i=0;i<itemEn.length;i++){
					fobCostDetail = new FobCostDetail();
					fobCostDetail.setFobCost(fobCost);
					fobCostDetail.setItemEn(itemEn[i]);
					fobCostDetail.setItemZn(itemZn[i]);
					fobCostDetail.setIsBaseonInvo(isBaseonInvo[i]);
					fobCostDetail.setAmount(amount[i]);
					fobCostDetail.setCreateTime(new Date());
					fobCostDetailList.add(fobCostDetail);
				}
				fobCost.setFobCostList(fobCostDetailList);
			}
			
			//保存主表信息
			fobCost.setIsTemplate(true);//增加模板标志
			fobCostService.saveFobCost(fobCost);
			
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.add"),this.getText("erp.business.fobcosttp.businame")});
			this.getResultMap().put("msg",msgStr);
		}catch(Exception e){
			//返回错误信息
			logger.error("添加FOB成本模板信息失败,失败原因:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.add"),this.getText("erp.business.fobcosttp.businame")});
			this.getResultMap().put("msg",msgStr);
		}
		return SUCCESS;
	}
	
	/**
	 * 到修改FOB成本模板页面
	 * @return SUCCESS
	 * @throws Exception 异常处理
	 */
	public String toEditFobCostTp() throws Exception{
		logger.debug("entry toEditFobCost method!...");
		try{
			logger.info("********************fobcostid="+fobCost.getId());
			Long fobCostId = fobCost.getId();
			fobCost = fobCostService.getFobCostById(fobCostId);
		}catch(Exception e){
			e.printStackTrace();
			logger.error("execute toEditFobCost page error!", e);
		}
		return SUCCESS;
	}
	
	/**
	 * 修改FOB成本模板信息
	 * @return
	 * @throws Exception
	 */
	public String editFobCostTp() throws Exception{
		logger.debug("entry editFobCost method!...");
		try{
			logger.info("fobCost="+fobCost);
			FobCost persistentFobCost = fobCostService.getFobCostById(fobCost.getId());
			
			fobCostDetailList = new ArrayList<FobCostDetail>();
			List<FobCostDetail> delFobCostDetailList = persistentFobCost.getFobCostList();
			
			if(itemEn != null){//针对没有细表记录的情况
				//设置细表信息
				for(int i=0;i<itemEn.length;i++){
					//新增的记录
					if((fobcostValueIds[i] == null) || ("".equals(fobcostValueIds[i]))){
						fobCostDetail = new FobCostDetail();
						fobCostDetail.setItemEn(itemEn[i]);
						fobCostDetail.setItemZn(itemZn[i]);
						fobCostDetail.setIsBaseonInvo(isBaseonInvo[i]);
						fobCostDetail.setAmount(amount[i]);
						fobCostDetail.setCreateTime(new Date());
						fobCostDetail.setFobCost(persistentFobCost);
						fobCostDetailList.add(fobCostDetail);
					}else{//修改的记录
						FobCostDetail persistentFobCostDetail = fobCostDetailService.getFobCostDetailById(fobcostValueIds[i]);

						persistentFobCostDetail.setItemEn(itemEn[i]);
						persistentFobCostDetail.setItemZn(itemZn[i]);
						persistentFobCostDetail.setIsBaseonInvo(isBaseonInvo[i]);
						persistentFobCostDetail.setAmount(amount[i]);
						persistentFobCostDetail.setFobCost(persistentFobCost);

						fobCostDetailList.add(persistentFobCostDetail);
					}
				}
				fobCost.setFobCostList(fobCostDetailList);
			}else{
				fobCost.setFobCostList(null);
			}
			
			//注:创建日期在这拷贝为空覆盖以前的,所有不拷贝创建日期
			BeanUtils.copyProperties(fobCost, persistentFobCost, new String[]{"isTemplate","createUser","createTime"});
			//修改信息
			fobCostService.editFobCost(persistentFobCost,delFobCostDetailList,fobcostValueIds);
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.edit"),this.getText("erp.business.fobcosttp.businame")});
			this.getResultMap().put("msg",msgStr);
		}catch(Exception e){
			//返回错误信息
			logger.error("修改FOB成本模板信息失败,失败原因:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.edit"),this.getText("erp.business.fobcosttp.businame")});
			this.getResultMap().put("msg",msgStr);
		}
		return SUCCESS;
	}
	
	/**
	 * 删除
	 * @return
	 * @throws Exception
	 */
	public String delFobCostTp() throws Exception{
		logger.debug("entry delFobCost method!...");
		try{
			logger.info("fobcostIds="+fobcostIds);
			String[] arrayIds = fobcostIds.split(",");
			fobCostService.delFobCost(arrayIds);
			//返回成功信息
			this.getResultMap().put("result", SUCCESS);
			String msgStr = this.getText("erp.opt.success.tip", new String[]{this.getText("erp.erp.delete"),this.getText("erp.business.fobcosttp.businame")});
			this.getResultMap().put("msg",msgStr);
		}catch(Exception e){
			logger.error("删除FOB成本模板信息失败,失败原因:" + e.getMessage(),e);
			//返回错误信息
			e.printStackTrace();
			this.getResultMap().put("result",ERROR);
			String msgStr = this.getText("erp.opt.fail.tip", new String[]{this.getText("erp.erp.delete"),this.getText("erp.business.fobcosttp.businame")});
			this.getResultMap().put("msg",msgStr);
		}
		return SUCCESS;
	}
	
	public String checkFobCostTp() throws Exception{
		logger.debug("entry checkFobCostTp method!...");
		try{
			this.getResultMap().put("checkFlag",true);
			if(StringUtils.isNotBlank(fobCost.getItem()) && StringUtils.isNotBlank(fobCost.getFobSize())){
				FobCost fob = fobCostService.getFobCostByItemAndSize(fobCost.getItem(),fobCost.getFobSize());
				if(null != fob){
					this.getResultMap().put("checkFlag",false);
				}
			}
		}catch(Exception e){
			//返回错误信息
			logger.error("entry checkFobCostTp method exception:" + e.getMessage(),e);
			this.getResultMap().put("result",ERROR);
		}
		this.getResultMap().put("result",SUCCESS);
		return SUCCESS;
	}
	
	@Override
	public FobCost getModel() {
		if(null == fobCost){
			fobCost = new FobCost();
		}
		return fobCost;
	}

	public FobCost getFobCost() {
		return fobCost;
	}

	public void setFobCost(FobCost fobCost) {
		this.fobCost = fobCost;
	}

	public String getFobcostIds() {
		return fobcostIds;
	}

	public void setFobcostIds(String fobcostIds) {
		this.fobcostIds = fobcostIds;
	}

	public String[] getItemEn() {
		return itemEn;
	}

	public void setItemEn(String[] itemEn) {
		this.itemEn = itemEn;
	}

	public String[] getItemZn() {
		return itemZn;
	}

	public void setItemZn(String[] itemZn) {
		this.itemZn = itemZn;
	}

	public Double[] getAmount() {
		return amount;
	}

	public void setAmount(Double[] amount) {
		this.amount = amount;
	}

	public Boolean[] getIsBaseonInvo() {
		return isBaseonInvo;
	}

	public void setIsBaseonInvo(Boolean[] isBaseonInvo) {
		this.isBaseonInvo = isBaseonInvo;
	}

	public List<FobCostDetail> getFobCostDetailList() {
		return fobCostDetailList;
	}

	public void setFobCostDetailList(List<FobCostDetail> fobCostDetailList) {
		this.fobCostDetailList = fobCostDetailList;
	}

	public Long[] getFobcostValueIds() {
		return fobcostValueIds;
	}

	public void setFobcostValueIds(Long[] fobcostValueIds) {
		this.fobcostValueIds = fobcostValueIds;
	}

	public List<FobCost> getFobCostList() {
		return fobCostList;
	}

	public void setFobCostList(List<FobCost> fobCostList) {
		this.fobCostList = fobCostList;
	}
}
